<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2019/1/21
 * Time: 10:34
 */

namespace api\storage\controller;


use api\storage\common\Constant;
use api\storage\service\CarService;
use think\Request;

class CarController extends AppBaseController
{

    private $service;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->service = new CarService();
    }

    /**
     * @api {GET} /api/storage/car/bom_time 1.获取出库单时间
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  getBomTime
     * @apiGroup  car
     * @apiVersion 0.0.1
     * @apiDescription 查找bom单的出库时间段
     *
     * @apiParam {string} bomId bom单号
     *
     * @apiSuccess {int} code 0
     * @apiSuccess {string} msg 获取成功
     * @apiSuccess {object} data
     * @apiSuccess {array} data.times 出库单时间段
     * @apiError  Error
     **/
    public function getBomTime()
    {
        $bomId = input('bomId');
        $result = $this->service->getBomTime($bomId, $this->getStorageUuid());
        $this->success('获取成功', $result['data']);
    }


    /**
     * @api {POST} /api/storage/car/car_count 2.获取装车数量
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  getCarCount
     * @apiGroup  car
     * @apiVersion 0.0.1
     * @apiDescription 根据条件，查询具体物料的信息
     *
     * @apiParam {array} params 请求的参数，如：[{"bomId":"bomId","time":["2019-01-08","2019-01-09"]}]
     *
     * @apiSuccess {int} code 0 | 231   当code = 231 直接进入装车
     * @apiSuccess {string} msg 获取成功
     * @apiSuccess {object} data
     * @apiSuccess {string} data.aCount A类物料的数量
     * @apiSuccess {string} data.bCount B类物料的数量
     * @apiError  Error
     **/
    public function getCarCount()
    {
        $data = $this->jsonDecode($_POST['params']);
        $result = $this->service->getCarCount($data, $this->getStorageUuid());
        switch ($result['status']) {
            case -1:
                $this->error('该出库单没有出库物料');
                break;
            case 1:
                $this->success('获取成功', $result['data']);
                break;
            case 2:
                $this->success('进入装车成功', [], Constant::RETURN_CODE_CAR_ENTRY);
                break;
            default:
                $this->error('请求失败');

        }
    }

    /**
     * @api {GET} /api/storage/car/car_norm 3.获取小车规格
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  getCarNorm
     * @apiGroup  car
     * @apiVersion 0.0.1
     * @apiDescription 获取小车规格，并且判断这个小车是否有物料信息
     *
     * @apiParam {string} carId 小车id
     *
     * @apiSuccess {int} code 0
     * @apiSuccess {string} msg 获取成功
     * @apiSuccess {object} data
     * @apiSuccess {string} data.aCount A类型站位的数量
     * @apiSuccess {string} data.bCount B类型站位的数量
     * @apiSuccess {string} data.hadData 1表示小车上有物料，0表示没有
     * @apiError  Error
     **/
    public function getCarNorm()
    {
        $carId = input('carId');
        $result = $this->service->getCarNorm($carId);
        switch ($result['status']) {
            case 0:
                $this->error('获取规格失败', $result['data']);
                break;
            case 1:
                $this->success('获取成功', $result['data']);
                break;
            default:
                $this->error('请求失败');
        }
    }

    /**
     * @api {POST} /api/storage/car/seat_distribute 4.分配站位
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  distributeSeatCode
     * @apiGroup  car
     * @apiVersion 0.0.1
     * @apiDescription 预先分配好站位
     *
     * @apiParam {array} carIds 小车id,如：["A01","A02"]
     * @apiParam {string} bomId bom单号，多个取扫描的第一个
     * @apiParam {int} isOverride 是否覆盖，覆盖为1  不覆盖（追加）为 0
     * @apiParam {int} status 小车的数量是否满足装车，1 满足 0 不满足
     *
     * @apiSuccess {int} code 0
     * @apiSuccess {string} msg 分配成功
     * @apiError  Error
     **/
    public function distributeSeatCode()
    {
        $carIds = $this->jsonDecode($_POST['carIds']);
        $bomId = input('bomId');
        $isOverride = input('isOverride');
        $status = input('status');
        $result = $this->service->distributeSeatCode($carIds, $bomId, $isOverride, $status, $this->getStorageUuid());
        switch ($result['status']) {
            case 1:
                $this->success('分配成功');
                break;
            default:
                $this->error('请求失败');
        }
    }

    /**
     * @api {GET} /api/storage/car/seat_code 5.获取小车站位
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  getSeatCode
     * @apiGroup  car
     * @apiVersion 0.0.1
     * @apiDescription 获取分配好的小车的站位信息
     *
     * @apiParam {string} bomId bomId
     * @apiParam {string} reelId reelId
     *
     * @apiSuccess {int} code 0
     * @apiSuccess {string} msg 获取成功
     * @apiSuccess {object} data
     * @apiSuccess {string} data.carId 小车编号
     * @apiSuccess {string} data.seatCode 站位号
     * @apiError  Error
     **/
    public function getSeatCode()
    {
        $bomId = input('bomId');
        $reelId = input('reelId');
        $result = $this->service->getSeatCode($bomId, $reelId, $this->getStorageUuid(), $this->getNickname());
        switch ($result['status']) {
            case -1:
                $this->error('该物料不是装车的物料');
                break;
            case 1:
                $this->success('获取成功', $result['data']);
                break;
            default:
                $this->error('请求失败');
        }
    }

    /**
     * @api {PUT} /api/storage/car/seat_put 6.1放入站位
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  putSeat
     * @apiGroup  car
     * @apiVersion 0.0.1
     * @apiDescription 将物料放在站位上
     *
     * @apiParam {string} bomId bomId
     * @apiParam {string} reelId reelId
     * @apiParam {string} seatCode 站位号
     *
     * @apiSuccess {int} code 0
     * @apiSuccess {string} msg 装车成功
     * @apiError  Error
     **/
    public function putSeat()
    {
        $bomId = input('bomId');
        $reelId = input('reelId');
        $seatCode = input('seatCode');
        $result = $this->service->putSeat($bomId, $reelId, $seatCode, $this->getStorageUuid(), $this->getNickname());
        switch ($result['status']) {
            case -1:
                $this->error('请扫描正确的站位');
                break;
            case 0:
                $this->error('装车失败');
                break;
            case 1:
                $this->success('装车成功');
                break;
            default:
                $this->error('请求失败');
        }
    }

    /***
     * @api {POST} /api/storage/car/cancel_put_seat 6.2取消放入站位
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  cancelPutSeat
     * @apiGroup  car
     * @apiVersion 0.0.1
     * @apiDescription  取消放入站位的动作，将站位写回到小车位置文件中
     *
     * @apiParam {string} bomId bomId
     * @apiParam {string} reelId reelId
     * @apiSuccess {int} code 0
     * @apiSuccess {string} msg 取消成功
     * @apiError  {int} code 100 取消失败
     */
    public function cancelPutSeat()
    {
        $bomId = input("bomId");
        $reelId = input("reelId");
        $result = $this->service->cancelPutSeat($bomId, $this->getStorageUuid(), $reelId);
        switch ($result['status']) {
            case 1:
                $this->success('取消成功');
                break;
            default:
                $this->error('请求失败');
        }
    }

    /**
     * @api {GET} /api/storage/car/reels_check 6.3.1确认物料
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  checkReels
     * @apiGroup  car
     * @apiVersion 0.0.1
     * @apiDescription 确认物料是否是装车的物料，一般用于容器
     *
     * @apiParam {string} bomId bomId
     * @apiParam {string} reelId reelId
     *
     * @apiSuccess {int} code 0
     * @apiSuccess {string} msg 确认成功
     * @apiError  Error
     **/
    public function checkReels()
    {
        $bomId = input('bomId');
        $reelId = input('reelId');
        $result = $this->service->checkReels($bomId, $reelId, $this->getStorageUuid());
        switch ($result['status']) {
            case -1:
                $this->error('该物料不是装车的物料');
                break;
            case 1:
                $this->success('确认成功');
                break;
            default:
                $this->error('请求失败');
        }
    }

    /**
     * @api {POST} /api/storage/car/container_put 6.3.2放入容器
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  putContainer
     * @apiGroup  car
     * @apiVersion 0.0.1
     * @apiDescription 将物料放在容器中
     *
     * @apiParam {string} bomId bomId
     * @apiParam {array} reelIds reelId的数组,如["",""]
     * @apiParam {string} containerCode 容器号
     *
     * @apiSuccess {int} code 0
     * @apiSuccess {string} msg 放置成功
     * @apiError  Error
     **/
    public function putContainer()
    {
        $bomId = input('bomId');
        $reelIds = $this->jsonDecode($_POST['reelIds']);
        $code = input('containerCode');
        $result = $this->service->putContainer($bomId, $reelIds, $code, $this->getStorageUuid());
        switch ($result['status']) {
            case 1:
                $this->success('放置成功');
                break;
            default:
                $this->error('请求失败');
        }
    }

    /**
     * @api {PUT} /api/storage/car/car_complete 7.完成装车
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  complete
     * @apiGroup  car
     * @apiVersion 0.0.1
     * @apiDescription 完成装车操作
     *
     * @apiParam {string} bomId bomId
     *
     * @apiSuccess {int} code 0
     * @apiSuccess {string} msg 完成装车
     * @apiError  Error
     **/
    public function complete()
    {
        $bomId = input('bomId');
        $result = $this->service->complete($bomId, $this->getStorageUuid(), $this->getNickname());
        switch ($result['status']) {
            case 1:
                $this->success('完成装车');
                break;
            default:
                $this->error('请求失败');
        }
    }


}